import { defineStore } from 'pinia'
import { ref } from 'vue'
import { queryProjectsService } from '@/api/project'

export const useProjectStore = defineStore(
  'love-project',
  () => {
    // 项目列表
    const projects = ref([])

    const fetchProjects = async (selection) => {
      const fetchedProjects = await queryProjectsService({
        pageSize: selection.pageSize,
        pageNum: selection.pageNum,
      })
      console.log(fetchedProjects)
      projects.value = fetchedProjects.data.data.items
      return fetchedProjects.data.data.total // 返回总数而不是直接修改selection
    }
    // 设置项目列表
    const getProjects = () => {
      return projects.value
    }
    const setProjects = (newProjects) => {
      projects.value = newProjects
    }

    const getProject = async ({
      pageSize,
      pageNum,
      projectDelectStatus,
      schoolId,
      auditStatus,
    }) => {
      return await queryProjectsService({
        pageSize: pageSize,
        pageNum: pageNum,
        //推广状态
        projectDelectStatus: projectDelectStatus,
        //学校id
        schoolId: schoolId,
        //审核状态 true审核项目 false未审核的项目
        auditStatus: auditStatus,
      })
    }

    //获取标记项目
    const getMarkedProjects = async ({ pageNum, pageSize }) => {
      return await queryProjectsService({
        pageSize: pageSize,
        pageNum: pageNum,
      })
    }

    return { projects, getProjects, fetchProjects, getProject, setProjects }
  },

  {
    persist: true,
    // 持久化存储的插件选项
  },
)
